#region --- License & Copyright Notice ---
/*
Copyright (c) 2005-2011 Jeevan James
All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#endregion

using System.ComponentModel;

namespace Id3.Net.Samples.BatchEditor
{
    public sealed class Id3TagSubset
    {
        private string _album;
        private string _artists;
        private string _composers;
        private string _title;
        private int _track;
        private int _year;

        private Id3TextEncoding _encodingType = Id3TextEncoding.Unicode;

        private bool _modified;

        public Id3TagSubset(Id3Tag tag)
        {
            if (tag == null)
                return;

            if (tag.Album.IsAssigned)
                _album = tag.Album.Value;
            if (tag.Artists.IsAssigned)
                _artists = string.Join(";", tag.Artists.Values.ToArray());
            if (tag.Composers.IsAssigned)
                _composers = string.Join(";", tag.Composers.Values.ToArray());
            if (tag.Title.IsAssigned)
                _title = tag.Title.Value;
            if (tag.Track.IsAssigned)
                _track = tag.Track.AsInt.Value;
            if (tag.Year.IsAssigned)
                _year = tag.Year.AsInt.Value;
        }

        public void SaveToTag(Id3Tag tag)
        {
            tag.Album.Value = _album;
            tag.Album.EncodingType = _encodingType;

            string[] artists = (_artists == null) ? new string[0] : _artists.Split(';');
            tag.Artists.Values.Clear();
            tag.Artists.Values.AddRange(artists);
            tag.Artists.EncodingType = _encodingType;

            string[] composers = (_composers == null) ? new string[0] : _composers.Split(';');
            tag.Composers.Values.Clear();
            tag.Composers.Values.AddRange(composers);
            tag.Composers.EncodingType = _encodingType;

            tag.Title.Value = _title;
            tag.Title.EncodingType = _encodingType;

            tag.Track.AsInt = _track;
            tag.Track.EncodingType = _encodingType;

            tag.Year.Value = _year.ToString();
            tag.Year.EncodingType = _encodingType;

            _modified = false;
        }

        [Browsable(false)]
        public bool Modified
        {
            get { return _modified; }
        }

        [Category("General Options")]
        public Id3TextEncoding EncodingType
        {
            get { return _encodingType; }
            set { _encodingType = value; }
        }

        #region Tag properties
        [Category("Main Info")]
        [Description("Album name")]
        public string Album
        {
            get { return _album; }
            set
            {
                _modified = true;
                _album = value;
            }
        }

        [Category("Main Info")]
        [Description("List of artists, separated by semi-colons")]
        public string Artists
        {
            get { return _artists; }
            set
            {
                _modified = true;
                _artists = value;
            }
        }

        [Category("Extra Info")]
        [Description("List of composers, separated by semi-colons")]
        public string Composers
        {
            get { return _composers; }
            set
            {
                _modified = true;
                _composers = value;
            }
        }

        [Category("Main Info")]
        [Description("Title of the song")]
        public string Title
        {
            get { return _title; }
            set
            {
                _modified = true;
                _title = value;
            }
        }

        [Category("Main Info")]
        [Description("Track number")]
        public int Track
        {
            get { return _track; }
            set
            {
                _modified = true;
                _track = value;
            }
        }

        [Category("Main Info")]
        [Description("Release year")]
        public int Year
        {
            get { return _year; }
            set
            {
                _modified = true;
                _year = value;
            }
        }
        #endregion
    }
}